package com.yuke.cloud.service.wmc.controller.frontend;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.config.Printconfig;
import com.yuke.cloud.service.wmc.entity.WmsPrinter;
import com.yuke.cloud.service.wmc.printHttpUtil.HttpUtils;
import com.yuke.cloud.service.wmc.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/print")
public class printMachineManager {
    @PostMapping("/add")
    @ApiOperation(httpMethod = "POST", value = "注册一台打印机到快宝云服务")
    public Wrapper add(WmsPrinter wmsPrinter) throws Exception {  // mod by wg 20190219 增加WmsPrinter参数
        String host = "http://kop.kuaidihelp.com";
        String path = "/api";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("app_id", Printconfig.userId);
        bodys.put("method", "cloud.print.do");
        String ts = System.currentTimeMillis() + "";
        bodys.put("ts", ts);
        String str = Printconfig.userId + "cloud.print.do" + ts + Printconfig.apikey;
        String sign = MD5.MD5(str, "utf-8");
        bodys.put("sign", sign);
        // bodys.put("data", "{\"action\":\"register.agent\",\"agent_key\":\"9676647413644282\",\" nickname\":\" 打印机002\",\" phone\":\"18203651629\"}");  // todo 写死，要从数据库中获取
        bodys.put("data", "{\"action\":\"register.agent\",\"agent_key\":\"" + wmsPrinter.getAuthCode()
                + "\",\" nickname\":\"" +  wmsPrinter.getPrinterName() + "\",\" phone\":\""
                + wmsPrinter.getMobileNo() + "\"}");
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//        System.out.println(EntityUtils.toString(response.getEntity()));
        return WrapMapper.ok(EntityUtils.toString(response.getEntity()));
    }

    @PostMapping("/delete")
    @ApiOperation(httpMethod = "POST", value = "删除注册一台打印机到快宝云服务")
    public Wrapper delete(String authCode) throws Exception{  // mod by wg 20190219 增加打印机授权码参数
        String host = "http://kop.kuaidihelp.com";
        String path = "/api";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
//根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("app_id", Printconfig.userId);
        bodys.put("method", "cloud.print.do");
        // mod by wg 20190219
        //bodys.put("ts", "1524209949");
        //bodys.put("sign", "bdf3b5f50865ac813cbdfd6c9b572b79");
        String ts = System.currentTimeMillis() + "";
        String str = Printconfig.userId + "cloud.print.do" + ts + Printconfig.apikey;
        String sign = MD5.MD5(str, "utf-8");
        bodys.put("sign", sign);
        //ACTION用来区分不用的操作，删除，查看，添加
//         bodys.put("data", "{" action":" delete.agent"," agent_key":" "}");
        bodys.put("data", "{\"action\":\"delete.agent\",\"agent_key\":\"" + authCode  + "\"}");

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
//获取response的body
//System.out.println(EntityUtils.toString(response.getEntity()));
            return WrapMapper.ok(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "删除注册一台打印机到快宝云服务")
    public Wrapper print(@RequestBody JSONObject jsonObject) throws Exception {
        String host = "http://kop.kuaidihelp.com";
        String path = "/api";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
//根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("app_id", "50001");
        bodys.put("method", "cloud.print.do");
        bodys.put("ts", "1524209949");
        bodys.put("sign", "bdf3b5f50865ac813cbdfd6c9b572b79");
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//        System.out.println(EntityUtils.toString(response.getEntity()));
//        System.out.println(response.toString());
//        System.out.println(EntityUtils.toString(response.getEntity()));
        return null;
    }


}